Partial Class pages_Settings_Setting : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property SettingId() As Integer
        Get
            Return CWeb.RequestInt("settingId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return SettingId <> Integer.MinValue
        End Get
    End Property
#End Region

#Region "Members"
    Private m_setting As SchemaMikeConnellMinistries.CSetting
#End Region

#Region "Data"
    Public ReadOnly Property [Setting]() As SchemaMikeConnellMinistries.CSetting
        Get
            If IsNothing(m_setting) Then
                If IsEdit Then
                    m_setting = CSetting.Cache.GetById(SettingId)
                    If IsNothing(m_setting) Then CSitemap.RecordNotFound("Setting", SettingId)
                Else
                    m_setting = New CSetting
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_setting
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.SettingEdit(Me.Setting.SettingId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Settings)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()

        'Page Title
        If IsEdit Then            
            Me.Title = "Setting Details"
            txtSettingValue.NavigateUrl = CSitemap.SettingValue(SettingId)
            txtSettingId.Visible = False

            AddMenuSide("Setting Value", CSitemap.SettingValue(SettingId))
        Else
            Me.Title = "Create New Setting"
            txtSettingValue.Visible = False
            Me.Form.DefaultFocus = txtSettingName.Textbox.ClientID
        End If


        ddSettings.DataSource = CSetting.Cache
        ddSettings.DataBind()
        If CSession.IsDeveloper Then CDropdown.BlankItem(ddSettings, "-- New Setting --")
        CDropdown.SetValue(ddSettings, SettingId)

        ddType.AddEnums(GetType(ESettingType))
                
        'Textbox logic
        Me.Form.DefaultButton = btnSave.UniqueID
        txtSettingName.OnReturnPress(btnSave)

        'Button Text
        btnDelete.Visible = IsEdit AndAlso CSession.IsDeveloper
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Setting"
        If IsEdit AndAlso CSession.IsDeveloper Then AddButton(CSitemap.SettingAdd(), "Create a new Setting")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadSetting()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveSetting()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.Setting.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
    Protected Sub ddSettings_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSettings.SelectedIndexChanged
        Response.Redirect(CSitemap.SettingEdit(CDropdown.GetInt(ddSettings)))
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadSetting()
        With Me.Setting
            txtSettingName.Text = .SettingName
            txtSettingId.ValueInt = .SettingId
            txtSettingValue.Text = .Value
            ddType.ValueInt = .SettingTypeId
        End With
    End Sub
    Protected Sub SaveSetting()
        With Me.Setting
            .SettingName = txtSettingName.Text
            .SettingTypeId = ddType.ValueInt
            If Not IsEdit Then
                .SettingId = txtSettingId.ValueInt
            End If

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region


End Class
